<template>
  <div style="height: 100%; width: 100%; position: relative">
      <div class="pagetop" ref="buttondiv">
          <div class="operation-list">
              <!-- <el-button @click="QueryData()" icon="el-icon-search" v-has="'PINQUERY'" type="primary">查询</el-button> -->
              <el-button icon="el-icon-set-up" @click="StartInterface()">{{ buttontxt }}</el-button>
              <el-button icon="el-icon-document" @click="SaveAll()" v-has="'PMODIFY'" :disabled="!changeflag">保存</el-button>
              <el-button icon="el-icon-document" @click="CancelChange()" v-has="'PMODIFY'" :disabled="!changeflag">取消</el-button>
          </div>
      </div>
      <div class="pagecenter">
          <rm-layout-left splitcolor="white" movingcolor="#00ffff" :splitwidth=5 fixsize="70%" :minfixsize=80>
              <template v-slot:fix>
                  <div style="height:100%">
                      <vxe-table :data="condTable" :loading="loading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :select-config="{ trigger: 'row' }" :sort-config="{ trigger: 'cell' }" @current-change="currentChangeEvent" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                          <vxe-table-column type="index" title="NO." width="40" align="center"></vxe-table-column>
                          <vxe-table-column field="condtype" title="提醒条件类型" width="120">
                              <template>
                                  <span>待审核报告</span>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column field="condsql" title="待审核附加条件SQL" width="300"></vxe-table-column>
                          <vxe-table-column field="rptunitname" title="仅提示以下报告单元" width="200" align="center"></vxe-table-column>
                          <vxe-table-column field="reportday" title="提醒几天内的报告" width="140" align="center"></vxe-table-column>
                          <vxe-table-column field="stopflag" title="停止使用" width="120" align="center">
                              <template v-slot="{row}">
                                  <el-checkbox v-model="row.stopflag" true-label="1" false-label="0" :disabled="true"></el-checkbox>
                              </template>
                          </vxe-table-column>
                      </vxe-table>
                  </div>
              </template>
              <div style="height:100%">
                  <el-tabs style="height: 100%" type="border-card" @tab-click="tab_getdata" v-model="tab_showflag">
                      <el-tab-pane label="详细信息" name="detail">
                          <table style="width:100%">
                              <tr>
                                  <td>提醒条件类型：</td>
                                  <td colspan="2">
                                      <el-select v-model="condinfo.condtype"  style="width:100%" @change="hasChange">
                                          <el-option v-for="(item,index) in condTypeOption" :key="index" :value="item.value" :label="item.label"></el-option>
                                      </el-select>
                                  </td>
                              </tr>
                              <tr>
                                  <td>待审核附加条件SQL：</td>
                                  <td colspan="2">
                                      <el-input type="textarea" v-model="condinfo.condsql" :rows="10" @change="hasChange" @dblclick.native="OpenTestSql()" style="width: 100%; float: left"  placeholder="双击打开待审核附加条件向导"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td>仅提示以下报告单元：</td>
                                  <td colspan="2">
                                      <rm-ddl-rptunit ref="rptunitids" prgno="LIS340" :multiple_choice="true" v-model="condinfo.rptunitids" clearable width="100%" @change="hasChange"></rm-ddl-rptunit>
                                  </td>
                              </tr>
                              <tr>
                                  <td>提醒几天内的报告：</td>
                                  <td colspan="2">
                                      <el-input style="width: 100%;" v-model="condinfo.reportday" @input="hasChange" ref="reportday"></el-input>
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="3">
                                      <el-checkbox v-model="condinfo.isloginuser" true-label="1" false-label="0" @change="hasChange">仅在检验者为当前登录用户时提醒</el-checkbox>
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="3">
                                      <el-checkbox v-model="condinfo.stopflag" true-label="1" false-label="0" @change="hasChange" :disabled="true">停用</el-checkbox>
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="3">
                                      <span style="color:blue;font-size:16px;">
                                          <br/>
                                          *1.报告单元可多选，如果不选则先按照本地检验选项【1113：本地危急值提醒报告单元】进行提醒，如果本地检验选项也未设置，再按照当前登录用户所属检验组进行提醒；
                                          <br/>
                                          <br/>
                                          *2.待审核除了结果全部已做外还会考虑以下报告单元选项，请注意：
                                          <br/>
                                          【2213：未审核和待审核标本去除标本】，一些特殊标本不视为待审核报告
                                          <br/>
                                          【2213.1：未审核和待审核标本不包含病人类型】
                                          <br/>
                                          【2426：保留结果为空的数据】，结果为空也可能为待审核报告
                                          <br/>
                                          【2605：申请项目对应可选报告项目未做时可视为待审核状态】
                                          <br/>
                                          【4701：报告必须经过签名后才能审核】，签名模式下的待审核
                                      </span>
                                  </td>
                              </tr>
                          </table>
                      </el-tab-pane>
                  </el-tabs>
              </div>
          </rm-layout-left>
      </div>
      <!--检验工作站提醒条件向导弹窗--->
      <el-dialog title="检验工作站提醒条件向导" v-el-drag-dialog :close-on-click-modal="false" :visible.sync="outerVisible" height="500px" width="780px">
          <el-dialog :append-to-body="true" :close-on-click-modal="false" :title="outerdialogtitle" :before-close="cancelinnerdialog" :visible.sync="dialogTableVisible" v-if="dialogx">
              <rm-pop-common @change="popcomchange" @cancel="cancelinnerdialog" :datatype="dataclass"></rm-pop-common>
          </el-dialog>
          <el-form>
              <el-row>
                  <div style="width: 100%; float: left">
                      <el-input type="textarea" :rows="5" v-model="dialogdata" id="topdiv"></el-input>
                  </div>
              </el-row>
              <el-row>
                  <div style="height: 440px; border: 1px solid #ccc; margin-top: 5px">
                      <div style="float:left; left: 5px; width:550px; height:100%;">
                          <div style="width: 100%; float: left; height: 40px">
                              <div style="width: 60%; float: left">
                                  <el-form>
                                      <el-form-item label="字段快速查找" label-width="120">
                                          <el-input v-model="quicklysearch" style="width: 200px" placeholder="代码、名称、所属表" @keyup.enter.native="PopListSearch"></el-input>
                                      </el-form-item>
                                  </el-form>
                              </div>
                              <div style="width: 40%; float: left; margin-top: 5px">
                                  <el-button @click="TestSql" icon="el-icon-save">测试sql</el-button>
                                  <el-button @click="savedialog" icon="el-icon-document">确 定</el-button>
                                  <el-button @click="outerVisible = false" :autofocus="true" icon="el-icon-close">取 消</el-button>
                                  <el-button @click="openExample()">示例</el-button>
                              </div>
                          </div>
                          <div style="width: 100%; float: left; height: 390px">
                              <vxe-table show-overflow ref="xTable2" height="auto" row-id="id" :select-config="{ reserve: true }" :data.sync="poptableData" show-header-overflow highlight-current-row highlight-hover-row resizable border @cell-click="popcurrentChangeEvent" size="mini">
                                  <vxe-table-column type="index" title="NO." width="40"></vxe-table-column>
                                  <vxe-table-column field="col_table" title="字段所属表" width="100" sortable header-align="center"></vxe-table-column>
                                  <vxe-table-column field="col_code" title="字段代码" width="130" sortable header-align="center"></vxe-table-column>
                                  <vxe-table-column field="col_name" title="字段名称" width="140" sortable header-align="center"></vxe-table-column>
                                  <vxe-table-column field="hasvaluelist" title="有无取值列表" width="120" sortable header-align="center">
                                      <template v-slot="{ row }">
                                          <el-button v-if="row.hasvaluelist" style="width: 100px; margin: 4px" @click.native="SelectColVal(row.col_code, row.col_name)">{{row.hasvaluelist}}</el-button>
                                      </template>
                                  </vxe-table-column>
                              </vxe-table>
                          </div>
                      </div>
                      <div style="float:left; width: 200px;">
                          <div style="text-align: center; position: realative">
                              <table style="text-align: center">
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('+')">+</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('-')">-</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('*')">*</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('/')">/</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('>')">&gt;</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('>=')">&gt;=</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('<')">&lt;</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('<=')"> &lt;=</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('(')">(</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate(')')">)</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('and')">AND</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('or')"> OR</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('=')">等于</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('<>')">不等于</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('not')">not</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('in')">in</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('like')">like</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 52.5px; margin: 4px" @click="operate('%')">%</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button style="width: 60px; margin: 4px" @click="operate('@userno')">当前用户</el-button>
                                      </td>
                                      <td>
                                          <el-button style="width: 60px; margin: 4px" @click="operate('@hospcode')">当前医院</el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td colspan="2" style="text-align: left;">
                                          <span style="color:blue;">*单击左侧列表即可把该字段加入sql中，</span>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td colspan="2" style="text-align: left;">
                                          <span style="color:blue;">点击按钮可选择该字段对应的值。</span>
                                      </td>
                                  </tr>
                              </table>
                          </div>
                      </div>
                  </div>
              </el-row>
          </el-form>
      </el-dialog>
      <el-dialog title="示例" v-el-drag-dialog :close-on-click-modal="false" :visible.sync="exampleVisible" width="780px">
          <div style="width: 18%; float: left; height: 500px; margin-bottom: 4px;">
              <vxe-table show-overflow height="auto" row-id="id" :select-config="{ reserve: true }" :data.sync="tableDataExample" show-header-overflow highlight-current-row highlight-hover-row resizable border @cell-click="exampleValChangeEvent" v-model="dialogdata" size="mini">
                  <vxe-table-column field="condition_name" title="条件名称" width="130" sortable header-align="center"></vxe-table-column>
                  <vxe-table-column field="condition_value" title="条件值" width="100" sortable header-align="center" v-if="false"></vxe-table-column>
              </vxe-table>
          </div>
          <div style="width: 80%; float: left; height: 500px; margin-left: 4px;margin-bottom: 4px;padding: 4px 4px 4px 4px;">
              {{ exampledata }}
          </div>
      </el-dialog>
  </div>
</template>

<script>
import XEUtils from "xe-utils";
export default {
  name: "LIS164",
  data() {
      return {
          changeflag: false,
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          loading: false,
          condTable: [],
          condinfo: {},
          rptunitOption: [],
          multichktitle: "",
          option1: [],
          option2: [],
          fieldname: "",
          tab_showflag: "detail",
          poptableData: [],
          poptempData: [],
          dialogdata: "",
          outerVisible: false,
          outerdialogtitle: "",
          dialogTableVisible: false,
          dialogx: false,
          dataclass: "",
          quicklysearch: "",
          exampledata: "",
          exampleVisible: false,
          tableDataExample: [
              {
                  condition_name: "加急门诊",
                  condition_value: "a.pat_typecode='3' and a.emer_flag = '1'",
              },
              {
                  condition_name: "检验者为当前用户",
                  condition_value: "a.report_user = '@userno'",
              },
              {
                  condition_name: "交班备注为空(customs1)",
                  condition_value: "isnull(a.custom1, '') = ''",
              },
              {
                  condition_name: "糖耐量全部",
                  condition_value: "exists (select 1 from req_detail z1 where z1.barcode = a.barcode and z1.req_itemcode in ('LIS01690','LIS01693','LIS01694','LIS01695'))",
              },
          ],
          condTypeOption: [
              {
                  label: "待审核报告",
                  value: "1",
              },
          ],
          buttontxt: "启用",
      };
  },
  computed: {
  },
  created() {
      this.InitOption(); //初始化查询
      this.QueryData();
  },
  methods: {
      InitOption() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS164",
                  method: "post",
                  data: {
                      action: "initoption",
                  },
              })
              .then((res) => {
                  this.loading = false;
                  this.rptunitOption = res.rptunitOption;
              });
      },
      QueryData() {
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS164",
                  method: "post",
                  data: {
                      action: "querycondinfo",
                  },
              })
              .then((res) => {
                  this.loading = false;
                  this.condTable = res.data;
                  if(this.condTable.length > 0){
                      let currow = this.condTable[0];
                      this.$refs.xTable.setCurrentRow(currow);
                      this.condinfo = {
                          condid : currow.condid,
                          condtype: currow.condtype,
                          rptunitids : currow.rptunitids+"",
                          condsql: currow.condsql,
                          reportday: currow.reportday,
                          stopflag: currow.stopflag,
                          isloginuser: currow.isloginuser,
                      };
                  }

              });
      },
      // AddCond() {
      //     this.changeflag = true;
      //     this.condinfo = {
      //         condtype: "1",
      //         rptunitids : "",
      //         condsql: "",
      //         reportday: 3,
      //         stopflag: "0",
      //         isloginuser: "1",
      //     };
      // },
      // DelCond() {
      //     let delrow = this.$refs.xTable.getCurrentRow();
      //     if (delrow) {
      //         this.$confirm("确定删除该条记录吗？", "提示", {
      //             confirmButtonText: "确定",
      //             cancelButtonText: "取消",
      //             type: "warning",
      //             dangerouslyUseHTMLString: true,
      //         })
      //             .then(() => {
      //                 this.$tool
      //                     .request({
      //                         url: "LIS/LIS1/LIS164",
      //                         method: "post",
      //                         data: {
      //                             action: "delcondinfo",
      //                             condid: delrow.condid,
      //                         },
      //                     })
      //                     .then((res) => {
      //                         if(res.flag == "1") {
      //                             this.$message.success("删除成功！");
      //                             this.QueryData();
      //                         } else {
      //                             this.$message.error("删除失败：" + res.msg);
      //                         }
      //                     });
      //             })
      //     }
      // },
      StartInterface() {
          if(this.condinfo.stopflag == "1"){
              this.condinfo.stopflag = "0";
          } else {
              this.condinfo.stopflag = "1";
          }
          this.SaveAll();
      },
      SaveAll() {
          if(this.condinfo.reportday > 7) {
              this.$alert("提醒天数过长，超过了7天，影响弹窗速度，请重新设置！");
              this.$refs.reportday.focus();
              return;
          }
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS164",
                  method: "post",
                  data: {
                      action: "savecondinfo",
                      condinfo: this.condinfo,
                  },
              })
              .then((res) => {
                  if(res.flag == "1") {
                      this.$message.success("保存成功！");
                      this.changeflag = false;
                      this.QueryData();
                  } else {
                      this.$message.error("保存失败：" + res.msg);
                  }
              });
      },
      CancelChange() {
          this.$confirm("此操作将取消所有改动，是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
          }).then(() => {
              this.QueryData();
          });
      },
      currentChangeEvent({ row }) {
          this.condinfo = {
              condid: row.condid,
              condtype: row.condtype,
              rptunitids : row.rptunitids+"",
              condsql: row.condsql,
              reportday: row.reportday,
              stopflag: row.stopflag,
              isloginuser: row.isloginuser,
          }
      },
      tab_getdata(tab, event) {
          if (tab.label == "详细信息") {
              this.tabindex = "1";
              if (this.tabindexlist.indexOf("1") <= -1) this.tabindexlist.push("1");
          }
      },
      hasChange() {
          this.changeflag = true;
      },
      OpenTestSql() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS164",
                  method: "post",
                  data: {
                      action: "inittablecolumn"
                  }
              })
              .then(res => {
                  this.poptableData = res.data;
                  this.poptempData = XEUtils.clone(res.data, true);
                  this.dialogdata = this.condinfo.condsql;
              });
          this.outerVisible = true;
      },
      cancelinnerdialog() {
          this.dialogTableVisible = false;
          this.dialogx = false;
      },
      popcomchange(c, v) {
          //"选择后code", c//"选择后name", v
          this.dialogx = false;
          if (this.dialogdata != "") {
              //如果是新增
              var myValue = " '"+ c +"' ";
              this.InputCurrentPos(myValue);
          } else {
              //修改
              this.dialogdata += " '"+ c +"' ";
          }
          this.dialogTableVisible = false;
      },
      PopListSearch() {
          let record = [];
          this.quicklysearch = this.quicklysearch.trim();
          if (this.quicklysearch != "") {
              record = this.poptempData.filter((item) => {
                  return (item.col_table ? item.col_table.toUpperCase().indexOf(this.quicklysearch.toUpperCase()) > -1 : false) || (item.col_code ?item.col_code.indexOf(this.quicklysearch) > -1 : false) || (item.col_name ? item.col_name.toUpperCase().indexOf(this.quicklysearch.toUpperCase()) > -1 : false);
              });
          } else {
              record = XEUtils.clone(this.poptempData, true);
          }
          this.poptableData = record;
      },
      TestSql() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS164",
                  method: "post",
                  data: {
                      action: "TestSql",
                      sql: this.dialogdata,
                  },
              })
              .then((res) => {
                  if (res.flag == "1") {
                      this.$alert("测试成功", "提示", { type: "success" });
                  } else {
                      this.$alert(res.msg, "提示", { type: "error" });
                  }
              });
      },
      savedialog() {
          this.condinfo.condsql = this.dialogdata;
          this.changeflag = true;
          this.outerVisible = false;
      },
      popcurrentChangeEvent({ row,column }) {
          //弹窗行选中事件
          if(column.property == "hasvaluelist") return;
          let sqlstr = "";
          switch(row.col_table) {
              case "报告表":
                  sqlstr = " a."+row.col_code;
                  break;
              case "申请表":
                  if(this.dialogdata.indexOf(" exists (select 1 from req_master") > -1) {
                      sqlstr = "req_master." + row.col_code;
                  } else {
                      sqlstr = " exists (select 1 from req_master (nolock) where req_master.barcode = a.barcode and req_master." + row.col_code + " )";
                  }
                  break;
              case "结果表":
                  if(this.dialogdata.indexOf(" exists (select 1 from lab_result") > -1) {
                      sqlstr = "lab_result." + row.col_code;
                  } else {
                      sqlstr = " exists (select 1 from lab_result (nolock) where lab_result.reportid = a.reportid and lab_result." + row.col_code + " )";
                  }
                  break;
          }
          this.InputCurrentPos(sqlstr);
      },
      SelectColVal(code, name) {
          this.dialogx = false;
          this.dataclass = "LIS3_" + code;
          this.dialogtitle = "请选择" + name;
          this.dialogx = true;
          this.dialogTableVisible = true;
      },
      //把插入的字段或值放到当前光标所在位置
      InputCurrentPos(myValue) {
          var myField = document.getElementById("topdiv");
          if (myField.selectionStart || myField.selectionStart === 0) {
              var startPos = myField.selectionStart;
              var endPos = myField.selectionEnd;
              this.dialogdata = myField.value.substring(0, startPos) + " " + myValue + " " + myField.value.substring(endPos, myField.value.length);
              this.$nextTick(() => {
                  myField.selectionStart = startPos + myValue.length + 2;
                  myField.selectionEnd = startPos + myValue.length + 2;
              }); // 这句是重点, 圈起来
              myField.focus();
              myField.setSelectionRange(myField, myField.selectionStart, myField.selectionEnd);
          } else {
              this.dialogdata += " " + myValue + " ";
          }
      },
      operate(text) {
          //按钮选中
          let tempvarible = " " + text + " ";
          if(text == "@userno" || text == "@hospcode") {
              tempvarible = " '" + text + "' ";
          }
          if (this.dialogdata != "") {
              this.InputCurrentPos(tempvarible);
          } else {
              this.dialogdata += tempvarible;
          }
      },
      openExample() {
          this.exampleVisible = true;
      },
      exampleValChangeEvent({ row, column }) {
          //示例弹窗行选中事件，显示对应值
          this.exampledata = row.condition_value;
      },
  },
  watch: {
          condinfo(val) {
              if (val) {
                  if (val.stopflag == "1") {
                      this.buttontxt = "启用";
                  } else {
                      this.buttontxt = "禁用";
                  }
              }
          }
      }
}
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  font-size: 12px;
  z-index: 99;
  overflow-x: auto;
  white-space: nowrap;
}
.operation-list {
  right: 5px;
  position: absolute;
  top: 5px;
  display: flex;
  align-items: center;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
</style>
